package cn.wolfcode.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    //查询列表
    @RequiresPermissions(value={"employee:list", "员工列表"},logical=Logical.OR)
    @RequestMapping("/list")
    //@ModelAttribute高级查询用回显
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){
        model.addAttribute("pageInfo",employeeService.query(qo));
        model.addAttribute("depts",departmentService.listAll());
        return "employee/list";
    }

    //删除
    @RequestMapping("/delete")
    public String delete(Long id){
        employeeService.delete(id);
        return "redirect:/employee/list.do";
    }

    //添加和修改跳转页面
    @RequestMapping("/input")
    public String input(Model model,Long id){
        model.addAttribute("departments",departmentService.listAll());
        if (id != null) {
            Employee employee = employeeService.get(id);
            model.addAttribute("employee",employee);
        }
        List<Role> roles = roleService.listAll();
        model.addAttribute("roles",roles);
        return "employee/input";
    }

    //添加和修改
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee,Long[] ids){
        try {
            if (employee.getId() != null) {
                employeeService.update(employee,ids);
            }else{
                //添加
                employee.setStatus(true);
                employeeService.save(employee,ids);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"失败");
        }
    }

    //批量删除
    @RequestMapping("/deleteBatch")
    @ResponseBody
    public JsonResult deleteBatch(Long[] ids){
        try {
            employeeService.deleteBatch(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"失败");
        }
    }

    //验证用户名是否存在
    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String,Boolean> checkName(String name,Long id){
        //通过判断id是否有值,如果有值,是编辑,没有值,是保存,
        HashMap<String,Boolean> map = new HashMap<>();
        if(id!=null){
            //编辑
            //根据id查询名称是否一致,是的话继续验证,不是的话中断
            Employee employee = employeeService.get(id);
            map.put("valid",employee.getName().equals(name));
            return map;
        }else{
            //保存,通过名字查找,如果是空的,就继续,不是空的就中断
            Employee employee = employeeService.selectByName(name);
            map.put("valid",employee==null);
            return map;
        }
    }

    //禁用或者恢复
    @RequestMapping("/stopWorkOrRenew")
    public String stopWorkOrRenew(Long id,boolean status){
        Employee employee = employeeService.get(id);
        employee.setStatus(!status);
        employeeService.update(employee,null);
        return "redirect:/employee/list.do";
    }

    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        //文件下载的响应头（让浏览器访问资源的的时候以下载的方式打开）
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        Workbook workbook = employeeService.exportXls();
        //输出流
        workbook.write(response.getOutputStream());

    }
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws Exception {
        employeeService.importXls(file);
        return new JsonResult();
    }
}
